package controllers.managers
{
	import mx.core.UIComponent;
	
	import controllers.commands.StartPositionCommand;
	
	import model.context.StartEleBoundaryProtocalContext;
	import model.vo.ProtocalEleBoundary;
	
	import view.mediator.MapUIEventMediator;
	import view.panel.ElectricBoundaryToolbar;
	import view.panel.LocationInfoPanel;

	/**
	 * 单例管理器 
	 * @author kant
	 * 
	 */	
	public class SingletonManager
	{
		private static var _instance : SingletonManager;
		private var _instanceMap : Object = {};
		
		public function SingletonManager()
		{
			if ( _instance )
			{
				throw new Error("不能直接实例化.");
			}
		}
		
		private static function getInst() : SingletonManager
		{
			if ( _instance == null )
			{
				_instance = new SingletonManager();
			}
			return _instance;
		} 
		
		/**
		 * 注册单例管理类 		
		 */		
		public static function init(view:UIComponent):void
		{
			initData();
			
			//注册UI管理器
			getInst().initUIList(view);
		}
		
		private static function initData():void
		{
			//注册单例
			getInst().register( LocationQueryManager.NAME, new LocationQueryManager());
			getInst().register( DataStoreManager.NAME, new DataStoreManager());
			getInst().register( SoundPlayerService.NAME, new SoundPlayerService());
			getInst().register( MapUIEventMediator.NAME, new MapUIEventMediator());
			getInst().register( StateManager.NAME, new StateManager());
			getInst().register( UIInstanceManager.NAME, new UIInstanceManager());
			getInst().register( ScheduleManager.NAME, new ScheduleManager());
			getInst().register( StartEleBoundaryProtocalContext.NAME, new StartEleBoundaryProtocalContext());
			getInst().register( ProtocalEleBoundary.NAME, new ProtocalEleBoundary());
			getInst().register( StartPositionCommand.NAME, new StartPositionCommand());
		}
		
		private function initUIList(view:UIComponent):void
		{
			var mainApp:Maper = Maper(view);
			//注册UI实体管理器
			var uiInst : UIInstanceManager = UIInstanceManager(SingletonManager.getServ(UIInstanceManager.NAME));
			uiInst.setView(LocationInfoPanel.NAME, mainApp.locInfoPanel);
			uiInst.setView(ElectricBoundaryToolbar.NAME, mainApp.toolbarElec);
		}
				
		/**
		 * 销毁资源
		 **/
		public static function destroy():void
		{
			//销毁调度器
			var taskmgr:ScheduleManager = ScheduleManager(getServ(ScheduleManager.NAME));
			taskmgr.clearTasks();
		}
		
		public function register( name:String, value:Object ):void
		{
			if ( _instanceMap[ name ] == null )
        	{
	        	_instanceMap[ name ] = value;         	
        	}
		}
		
		public function getServiceInstance( name:String ) : Object
		{
			return   _instanceMap[ name ] ;	 
		}
		
		public static function getServ( name:String ) : Object
		{
			return getInst().getServiceInstance( name );
		}
		
		/**
		 * 保存
		 **/
		public static function setServ(name:String, value:Object):void
		{
			getInst().register(name, value);
		}
	}
}